package cn.qu.netty.boot;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.qu.util.TypeUtils;

public class HandlerMapping {

	private Object controller;
	private String path;
	private HttpMethod httpMethod;
	private Method method;
	private List<Parameter> parameterList;
	private List<Parameter> pathVarParams;
	private Parameter pojoParam;

	public HandlerMapping(Object controller, String path, HttpMethod httpMethod, Method method) {
		this.controller = controller;
		this.path = path;
		this.httpMethod = httpMethod;
		this.method = method;
		List<String> pathVarParamNames = AbstractParameterResolver.resolvePathParam(path);
		parameterList = Arrays.stream(method.getParameters()).filter(param -> {
			if (TypeUtils.isPojo(param.getType())) {
				pojoParam = param;
				return false;
			}
			return true;
		}).collect(Collectors.toList());
		pathVarParams = parameterList.stream().filter(param -> pathVarParamNames.contains(param.getName()))
				.collect(Collectors.toList());
	}

	public Object getController() {
		return controller;
	}

	public String getPath() {
		return path;
	}

	public HttpMethod getHttpMethod() {
		return httpMethod;
	}

	public Method getMethod() {
		return method;
	}

	public List<Parameter> getPathVarParams() {
		return pathVarParams;
	}

	public List<Parameter> getParameterList() {
		return parameterList;
	}

	public Parameter getPojoParam() {
		return pojoParam;
	}

}
